import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';
import 'package:flutter_ncsjqz_getx/constant/keys.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/user_detail_entity.dart';
import 'package:flutter_ncsjqz_getx/page/all_farm/all_farm_view.dart';
import 'package:flutter_ncsjqz_getx/page/login/login_view.dart';
import 'package:flutter_ncsjqz_getx/page/my/email_phone_pwd/email_password_view.dart';
import 'package:flutter_ncsjqz_getx/page/my/my_view.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/conversation_list/rong_colud_session_list_view.dart';
import 'package:flutter_ncsjqz_getx/page/square/square_view.dart';
import 'package:flutter_ncsjqz_getx/util/permission_util.dart';
import 'package:flutter_ncsjqz_getx/widget/bottom_nav_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:get/get.dart';
import 'package:module_shop/main.dart';

class HomeLogic extends GetxController {
  /// todo：当前菜单索引
  int currentPageIndex = 2;
  /// todo：网络监听器
  var _subscription;
  /// todo：设置菜单索引
  setIndex(value) {
    // 商场和生态园不需要登录可进入
    if(SpUtil.getObject(Keys.userDataInfo) == null && value !=1 && value !=2){
      print("未登录，从闪屏进入登录页");
      return Get.off(()=>LoginPage());
    }
    currentPageIndex = value;
    update();
  }
  /// todo：菜单对应页面
  List<Widget> bodyData = [
    RongColudSessionListPage(),
    ShopPage(),
    AllFarmPage(),
    SquarePage(),
    MyPage(),
  ];
  /// todo：菜单数据
  List<BottomNavModel> menuData =[
    BottomNavModel(
      img: "assets/image/sj_select.png",
      unImg: "assets/image/sj.png",
      label: "聊天",
      color: Color(0xFF81955F),
      unColor: Color(0xFF959595),
    ),
    BottomNavModel(
      img: "assets/image/nc_select.png",
      unImg: "assets/image/nc.png",
      label: "商城",
      color: Color(0xFF81955F),
      unColor: Color(0xFF959595),
    ),
    BottomNavModel(
      img: "assets/image/sync_select.png",
      unImg: "assets/image/sy_nc.png",
      label: "生态园",
      color: Color(0xFF81955F),
      unColor: Color(0xFF959595),
    ),
    BottomNavModel(
      img: "assets/image/gc_select.png",
      unImg: "assets/image/gc.png",
      label: "分享",
      color: Color(0xFF81955F),
      unColor: Color(0xFF959595),
    ),
    BottomNavModel(
      img: "assets/image/my_select.png",
      unImg: "assets/image/my.png",
      label: "我的",
      color: Color(0xFF81955F),
      unColor: Color(0xFF959595),
    ),
  ];

  init() async {
    checkNetStatus(); 
    /// 获取定位权限
    await PermissionUtil.getLocation();
    getUserInfo();
  }

  /// todo： 登录后 获取用户信息
  getUserInfo() async{
    if(SpUtil.getObject(Keys.userDataInfo) != null){
      // 获取用户信息
      UserDetailEntity data = await DioService<UserDetailEntity>().queryPersonalDetail();
      if(data.code == 0) {
        Constant.userDetailData = data.data;
        if(data.data!.telPhone == null || data.data!.telPhone == ""){
          Get.off(()=>EmailPhonePasswordPage(title: "设置手机号",type: EPW.phone,emailPhone: "",qz: true,),);
        }
      }
    }
  }

  /// todo：检测网络连通性
  checkNetStatus() async{
    //_subscription首次无法检测，后续由_subscription监听
    //------------------------------------------------
    var connectivityResult = await (Connectivity().checkConnectivity());
    setNetStatus(connectivityResult);
    //-------------------------------------------------
    // 网络状态监听器
    _subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      setNetStatus(result);
    });
  }

  /// todo：设置网络状态
  setNetStatus(result){
    if (result == ConnectivityResult.mobile) {
      print("连入移动网络:$result");
      Constant.netStatus = result.toString();
    } else if (result == ConnectivityResult.wifi) {
      print("连入WIFI网络:$result");
      Constant.netStatus = result.toString();
    }else{
      print("未连接网络:$result");
      Constant.netStatus = result.toString();
    }
    update();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    if(_subscription!=null) _subscription.cancel();
  }

}
